package com.web_bloks.mvc.client;

import java.util.Enumeration;
import java.util.Vector;

/**
 * 
 * @author Jean-Lou Dupont
 *
 */
public class Observers {

	private Vector<IObserver> observers;
	
	private String notificationName;

	/**
	 * Constructor
	 * 
	 * @param note
	 * @param observer
	 */
	public Observers( String note, IObserver observer )	{
		
		if (note != null && observer != null) {
			this.observers = new Vector<IObserver>();
			this.observers.addElement( observer );
			this.notificationName = note;
		} else {
			throw new NullPointerException();
		}
	}

	/**
	 * Returns all observers
	 * 
	 * @return <code>Hashtable</code>
	 */
	public Vector<IObserver> getObservers( )
	{
		return this.observers;
	}

	/**
	 * Add new observer
	 * 
	 * @param observer
	 */
	public void addObserver( IObserver observer )
	{
		if (observer != null) {
			this.observers.addElement( observer );
		} else {
			throw new NullPointerException();
		}
	}

	/**
	 * Delete an observer
	 * 
	 * 
	 * @param observer
	 */
	public void deleteObserver( IObserver observer ) {
		
		if (observer != null) {
			this.observers.removeElement( observer );
		} else {
			throw new NullPointerException();
		}
	}

	/**
	 * Notify all observers of the notification
	 * 
	 * @param note
	 */
	public void notifyObservers( INotification note ) {
		
		if (note != null) {
			IObserver temp = null;
			for (Enumeration<IObserver> enu = this.observers.elements(); enu.hasMoreElements();) {
				temp = (IObserver) enu.nextElement();
				temp.notifyObserver( note );
			}
		} else {
			throw new NullPointerException();
		}
	}

	/**
	 * Return the notification
	 * 
	 * @return String
	 */
	public String getNotification( ) {
		return this.notificationName;
	}

}
